package com.demo.app.sort;

/**
 * @program: couple-game
 * @description: 有序数组中找到最右边的位置的数字
 * @author: fbl
 * @create: 2025-05-14 16:02
 **/
public class MostRightNum {

    public static void main(String[] args) {
        int[] arr = { 2, 2, 4, 4, 6, 7, 10, 10};
        int i = mostRightNum(arr, 11);
        if(i < 0){
            System.out.println("下标数字不存在" + i);
        }else{
            System.out.println("下标：" + i + " 数字：" + arr[i]);
        }

        System.out.println("===========");
        // 值传递和引用传递问题
        Node n1 = new Node(1);
        n1.next = new Node(2);
        n1.next.next = new Node(3);

//        n1 = n1.next;
        f(n1);

        System.out.println("main方法中n1的值：" + n1.value);
    }

    public static void f(Node n1){
        n1 = n1.next;
        System.out.println("副本n1的值：" + n1.value);
    }

    // 最右边数字下标
    private static int mostRightNum(int[] arr, int target) {
        int ans = -1;
        if(arr == null || arr.length == 0 || target < arr[0] || target > arr[arr.length - 1]){
            return ans;
        }
        int L = 0;
        int R = arr.length - 1;

        while(L <= R){
            int mid = (L + R) / 2;
            if(arr[mid] <= target){
                L = mid + 1;
            }else {
                R = mid - 1;
            }
        }
        return ans;
    }


    private static class Node{
        private Node next;
        private Integer value;

        public Node(Integer value){
            this.value = value;
        }
    }
}
